import { loginout, isloginreq ,putownpwd,getUserId} from 'services/app';
import { menu } from '../mock/menu';
import { routerRedux } from 'dva/router';
import { SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION } from 'constants';
export default {
    namespace: 'app',
    state: {
        user:{username:''},
        menu,
        loader: true,
        permissions: {
            visit: menu.map(item => item.id),
        },
        menuPopoverVisible: false,
        siderFold: window.localStorage.getItem('siderFold') === 'true',
        darkTheme: window.localStorage.getItem('darkTheme') === 'true',
        isNavbar: document.body.clientWidth < 769,
        navOpenKeys: JSON.parse(window.localStorage.getItem('navOpenKeys')) || [],
        locationPathname: '',
        locationQuery: {},
    },

    subscriptions: {

        setupHistory ({ dispatch, history }) {
            history.listen((location) => {
                location.pathname !== '/login' && dispatch({ type: 'islogin' });
                if(location.pathname !== '/login' && location.pathname !== '/'){
                    // dispatch({type:"getUserId"});
                }
            });
        },

        setup ({ dispatch }) {
            window.onresize = () => {
                const tid = setTimeout(() => {
                    dispatch({ type: 'changeNavbar' });
                    clearTimeout(tid);
                }, 300);
            };
        },

    },
    effects: {
        // 登出函数
        * logout ({ payload }, { call, put }) {
            localStorage.removeItem("userInfor");
            const data = yield call(loginout);
            if(data.code == 0){
                yield put(routerRedux.push('/login'));
            }
        },
        //修改密码
        *gotoUserInfo ({ payload }, { call, put }){
            const data = yield call(putownpwd, payload);
            if(data.code == 0){
                yield put(routerRedux.push('/login'));
            }
        },
        * loaderFun ({ payload }, { call, put }) {
            yield put({ type: 'updateState', payload: { loader: false } });
        },
        // 改变菜单函数
        * changeNavbar (action, { put, select }) {
            const { app } = yield (select(_ => _));
            const isNavbar = document.body.clientWidth < 769;
            yield put({ type: 'dashboard/save', payload: { bodyWidth: document.body.clientWidth } });
            if (isNavbar !== app.isNavbar) {
                yield put({ type: 'handleNavbar', payload: isNavbar });
            }
        },
        //路由跳转
        *goToPath ({ payload }, { put }){
            yield put(routerRedux.push('/' + payload));
        },
        //获取用户id
        *getUserId ({ payload }, { put ,call }){
            const data = yield call(getUserId);
            localStorage.setItem("userNameId",data);
        },
        //输入等于输入
        *inputAndOut ({ payload }, { put }){
            yield put({ type: 'updateState', payload: payload });
        },
    },
    reducers: {
        updateState (state, { payload }) {
            return {
                ...state,
                ...payload,
            };
        },

        switchSider (state) {
            window.localStorage.setItem('siderFold', !state.siderFold);
            return {
                ...state,
                siderFold: !state.siderFold,
            };
        },

        switchTheme (state) {
            window.localStorage.setItem('darkTheme', !state.darkTheme);
            return {
                ...state,
                darkTheme: !state.darkTheme,
            };
        },

        switchMenuPopver (state) {
            return {
                ...state,
                menuPopoverVisible: !state.menuPopoverVisible,
            };
        },

        handleNavbar (state, { payload }) {
            return {
                ...state,
                isNavbar: payload,
            };
        },

        handleNavOpenKeys (state, { payload: navOpenKeys }) {
            return {
                ...state,
                ...navOpenKeys,
            };
        },
    },
};

// import { menu } from '../mock/menu';
// //添加子菜单的二级页面
// *changeView ({ payload }, { call, put }){
//     menu.push({
//         id: '100',
//         bpid: '4',
//         icon: 'book',
//         name: '22222',
//         none:true,
//         route: '/lalalla',
//     });
//     yield put({type: 'app/updateState', payload: {menu:menu}});
// },